package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.Random;

import cn.kailangtu.commonevents.eventEntity.AutoLockEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardStopActionEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardStopCloseEvent;
import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.PasswordUtils;
import cn.kailangtu.commontools.ToastUtils;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardStopContext;
import cn.kailangtu.dataspot.data.context.SelectContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardStopBinding;
import cn.kailangtu.dataspot.status;

public class KeyboardStopFragment extends KeyboardBaseFragment {

    private FragmentKeyboardStopBinding binding;
    int random =  1000 + new  Random().nextInt(10000 - 1000 + 1);
    boolean lock = false;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardStopBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
        reset();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    private void initListener(){
        binding.btnSwitchStopCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!lock) {
                    //binding.rlSwitchStop.setVisibility(View.GONE);
                    EventBus.getDefault().post(new KeyboardStopCloseEvent());
                    status.isStop = false;
                }
            }
        });
        binding.btnSwitchStopSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(lock)
                {
                    String pwd = binding.tvSwitchPwd1.getText().toString()
                            + binding.tvSwitchPwd2.getText().toString()
                            + binding.tvSwitchPwd3.getText().toString()
                            + binding.tvSwitchPwd4.getText().toString();

                    if(APPSPUtils.getInstance().getStopPwd().equals(pwd))
                    {
                        lock = false;
                    }
                    else
                    {
                        String str = PasswordUtils.toPwd(random);
                        if(pwd.equals(str))
                        {
                            lock = false;
                        }
                        else {

                            Log.d("PPPPPPPPP","local:"+str+"  pwd:"+pwd);
                        }
                    }

                    if(!lock){




//                        binding.tvSwitchPwdTip.setVisibility(View.GONE);
//                        binding.tvSwitchPwd1.setText("0");
//                        binding.tvSwitchPwd2.setText("0");
//                        binding.tvSwitchPwd3.setText("0");
//                        binding.tvSwitchPwd4.setText("0");
//                        binding.btnSwitchStopCancel.setVisibility(View.VISIBLE);
//
//                        binding.rlSwitchStop.setVisibility(View.GONE);
//                        status.isStop = false;
//                        random =  1000 + new Random().nextInt(10000 - 1000 + 1);
                          EventBus.getDefault().post(new KeyboardStopCloseEvent());
                          APPSPUtils.getInstance().putStop(false);
                    }
                    else
                    {
                        ToastUtils.ShowInCenter(getContext(),"密码错误");

                    }
                }
                else{
                    String pwd = binding.tvSwitchPwd1.getText().toString()
                            + binding.tvSwitchPwd2.getText().toString()
                            + binding.tvSwitchPwd3.getText().toString()
                            + binding.tvSwitchPwd4.getText().toString();

                    if(pwd.equals("0000"))
                    {
                        ToastUtils.ShowInCenter(getContext(),"密码错误");
                    }
                    else
                    {
                        APPSPUtils.getInstance().putStopPwd(pwd);
                        APPSPUtils.getInstance().putStop(true);
                        binding.rlSwitchStop.setBackgroundResource(R.drawable.keyboard_stop_lock_bg);
                        binding.tvSwitchPwdTip.setVisibility(View.VISIBLE);

                        binding.tvSwitchPwd1.setText("0");
                        binding.tvSwitchPwd2.setText("0");
                        binding.tvSwitchPwd3.setText("0");
                        binding.tvSwitchPwd4.setText("0");
                        binding.btnSwitchStopCancel.setVisibility(View.GONE);
                        lock = true;
                    }
                }
            }
        });
        binding.tvSwitchPwd1.setOnClickListener(pwdOnClickListener);
        binding.tvSwitchPwd2.setOnClickListener(pwdOnClickListener);
        binding.tvSwitchPwd3.setOnClickListener(pwdOnClickListener);
        binding.tvSwitchPwd4.setOnClickListener(pwdOnClickListener);
    }



    View.OnClickListener pwdOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tv = (TextView)v;
            int currValue = Integer.parseInt(tv.getText().toString());
            currValue++;
            if(currValue > 9)
            {
                currValue = 0;
            }
            tv.setText(String.valueOf(currValue));
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardStopActionEvent event){
        keyboard(event.getActionValue());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AutoLockEvent event){
        autoLock();
    }

    private void keyboard(int action){
        switch(action) {
            case KeyboardAction.OPEN:
                if(!APPSPUtils.getInstance().isStop()) {
                    reset();
                }
                break;
            case KeyboardAction.ENTRY:
                if(KeyboardStopContext.getSelectedView() != null)
                {
                    KeyboardStopContext.getSelectedView().callOnClick();
                }
                break;
            case KeyboardAction.LEFT:
                Log.d("DDDDDDDDDDDDD","LEFT:进来了");
                if(KeyboardStopContext.getSelectedView() == null)
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd1);
                }
                else if(binding.tvSwitchPwd1.equals( KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.btnSwitchStopSave);
                }
                else if( binding.btnSwitchStopSave.equals(KeyboardStopContext.getSelectedView()))
                {
                    if(binding.btnSwitchStopCancel.getVisibility() == View.VISIBLE) {
                        KeyboardStopContext.setSelectedView(binding.btnSwitchStopCancel);
                    }
                    else
                    {
                        KeyboardStopContext.setSelectedView(binding.tvSwitchPwd4);
                    }
                }
                else if( binding.btnSwitchStopCancel.equals( KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd4);
                }
                else if(binding.tvSwitchPwd4.equals(KeyboardStopContext.getSelectedView() ))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd3);
                }
                else if(binding.tvSwitchPwd3.equals( KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd2);
                }
                else
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd1);
                }


                break;
            case KeyboardAction.RIGHT:

                if( binding.tvSwitchPwd1.equals(KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd2);
                }
                else if( binding.tvSwitchPwd2.equals( KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd3);
                }
                else if( binding.tvSwitchPwd3.equals(KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd4);
                }
                else if( binding.tvSwitchPwd4.equals( KeyboardStopContext.getSelectedView()))
                {
                    if(binding.btnSwitchStopCancel.getVisibility() == View.VISIBLE) {
                        KeyboardStopContext.setSelectedView(binding.btnSwitchStopCancel);
                    }
                    else {
                        KeyboardStopContext.setSelectedView(binding.btnSwitchStopSave);
                    }
                }
                else if( binding.btnSwitchStopCancel.equals(KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.btnSwitchStopSave);
                }
                else if( binding.btnSwitchStopSave.equals(KeyboardStopContext.getSelectedView()))
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd1);
                }
                else
                {
                    KeyboardStopContext.setSelectedView(binding.tvSwitchPwd1);
                }

                break;
        }
    }

    public void reset()
    {
        Log.d("SSSSSSSSSSSSS","reset");
        binding.tvSwitchPwd1.setText("0");
        binding.tvSwitchPwd2.setText("0");
        binding.tvSwitchPwd3.setText("0");
        binding.tvSwitchPwd4.setText("0");
        random =  1000 + new  Random().nextInt(10000 - 1000 + 1);
        binding.tvSwitchPwdTip.setText("密钥："+random);
        binding.tvSwitchPwdTip.setVisibility(View.GONE);
        binding.btnSwitchStopCancel.setVisibility(View.VISIBLE);
        if(!APPSPUtils.getInstance().isStop()) {
            APPSPUtils.getInstance().putStopPwd("");
        }
        binding.rlSwitchStop.setBackgroundResource(R.drawable.keyboard_stop_bg);

    }

    public void autoLock()
    {
        Log.d("SSSSSSSSSSSSS","autoLock");
        binding.rlSwitchStop.setBackgroundResource(R.drawable.keyboard_stop_lock_bg);
        binding.tvSwitchPwdTip.setVisibility(View.VISIBLE);

        binding.tvSwitchPwd1.setText("0");
        binding.tvSwitchPwd2.setText("0");
        binding.tvSwitchPwd3.setText("0");
        binding.tvSwitchPwd4.setText("0");
        binding.btnSwitchStopCancel.setVisibility(View.GONE);
        lock = true;
    }
}
